﻿package com.athena.controller
{	
	import com.athena.vo.Chess;
	import com.athena.process.*;
	public class P_Controller {
		
		function P_Controller() { }
		public static function CheckTuong(hor : int, ver : int, csCheck : int) {
			//ProcessInTable.tf.appendText("==>CT:" + hor + "-" + ver) ;
			for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess.length; i++) 
			{
				if (ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess[i].Status == 1) continue ;
				if ((ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess[i].Hor == hor) && (ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess[i].Ver == ver)) {
					if (ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess[i].Symbol == "Tg") return true ;
					else return false ;
				}							
			}
			return false ;
		}
		public static function Check_ChieuTuong(obj : Chess, csCheck : int) : Boolean {
			var hor : int = obj.Hor ;
			var ver : int = obj.Ver ;
			//{Len tren
			while (hor > 0) {
				hor = hor - 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					//ProcessInTable.tf.appendText("==>Tim an:" + hor) ; 
					while (hor > 0) {
						hor = hor - 1 ;
						//ProcessInTable.tf.appendText("==>color:" + ProcessInTable.ArrCheck[hor][ver]) ; 						
						if ((ProcessInTable.ArrCheck[hor][ver] != obj.Color) && (ProcessInTable.ArrCheck[hor][ver] != 0)) {
							if (CheckTuong(hor, ver, csCheck)) return true ;
							break ;
						}
					}
					break ;
				}
			}
			//}
			//{Xuong duoi
			hor = obj.Hor ;
			ver = obj.Ver ;
			while (hor < 9) {
				hor = hor + 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					while (hor < 9) {
						hor = hor + 1 ;
						if ((ProcessInTable.ArrCheck[hor][ver] != obj.Color) && (ProcessInTable.ArrCheck[hor][ver] != 0)) {
							if (CheckTuong(hor, ver, csCheck)) return true ;
							break ;
						}
					}
					break ;
				}
			}
			//}
			//{Sang trai
			hor = obj.Hor ;
			ver = obj.Ver ;
			while (ver < 8) {
				ver = ver + 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					while (ver < 8) {
						ver = ver + 1 ;
						if ((ProcessInTable.ArrCheck[hor][ver] != obj.Color) && (ProcessInTable.ArrCheck[hor][ver] != 0)) {
							if (CheckTuong(hor, ver, csCheck)) return true ;
							break ;
						}
					}
					break ;
				}
			}
			//}
			//{Sang phai
			hor = obj.Hor ;
			ver = obj.Ver ;
			while (ver > 0) {
				ver = ver - 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] != 0) {
					while (ver > 0) {
						ver = ver - 1 ;
						if ((ProcessInTable.ArrCheck[hor][ver] != obj.Color) && (ProcessInTable.ArrCheck[hor][ver] != 0)) {
							if (CheckTuong(hor, ver, csCheck)) return true ;
							break ;
						}
					}
					break ;
				}
			}
			//}
			return false ;
		}
		public static function Search_Chess(hor : int, ver : int) {
			//ProcessInTable.tf.appendText("Search Chess:" + hor + "-" + ver) ;
			for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess.length; i++) 
			{
				if (ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Status == 1) continue ;
				if ((ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Hor == hor) && (ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Ver == ver)) {
					ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].gotoAndStop(3) ;
					if (ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Symbol == "Tg") {
						ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].Xet_mc.rotation = 180 ;
						ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].Xet_mc.x = 0 ;
						ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].Xet_mc.y = 15 ;
					}
					break ;
				}							
			}
		}
		public static function Valid(obj : Chess) {
			var hor : int = obj.Hor ;
			var ver : int = obj.Ver ;
			//{Len tren
			while (hor > 0) {
				hor = hor - 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] == 0) {
					Cocaro.mainStage_mc["Checked" + hor.toString() + ver.toString() + "_mc"].visible = true ;
					Cocaro.mainStage_mc["Checked" + hor.toString() + ver.toString() + "_mc"].over_mc.visible = false ;
				} else {
					while (hor > 0) {
						hor = hor - 1 ;
						if (ProcessInTable.ArrCheck[hor][ver] != 0)	{
							if (ProcessInTable.ArrCheck[hor][ver] != obj.Color)
								Search_Chess(hor, ver) ;
							break ;
						}
					}
					break ;
				}
			}
			//}
			//{Xuong duoi
			hor = obj.Hor ;
			ver = obj.Ver ;
			while (hor < 9) {
				hor = hor + 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] == 0) {
					Cocaro.mainStage_mc["Checked" + hor.toString() + ver.toString() + "_mc"].visible = true ;
					Cocaro.mainStage_mc["Checked" + hor.toString() + ver.toString() + "_mc"].over_mc.visible = false ;
				} else {
					while (hor < 9) {
						hor = hor + 1 ;
						if (ProcessInTable.ArrCheck[hor][ver] != 0) {
							if (ProcessInTable.ArrCheck[hor][ver] != obj.Color)
								Search_Chess(hor, ver) ;
							break ;
						}
					}
					break ;
				}
			}
			//}
			//{Sang trai
			hor = obj.Hor ;
			ver = obj.Ver ;
			while (ver < 8) {
				ver = ver + 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] == 0) {
					Cocaro.mainStage_mc["Checked" + hor.toString() + ver.toString() + "_mc"].visible = true ;
					Cocaro.mainStage_mc["Checked" + hor.toString() + ver.toString() + "_mc"].over_mc.visible = false ;
				} else {
					while (ver < 8) {
						ver = ver + 1 ;
						if (ProcessInTable.ArrCheck[hor][ver] != 0) {
							if (ProcessInTable.ArrCheck[hor][ver] != obj.Color) 
								Search_Chess(hor, ver) ;
							break ;
						}
					}
					break ;
				}
			}
			//}
			//{Sang phai
			hor = obj.Hor ;
			ver = obj.Ver ;
			while (ver > 0) {
				ver = ver - 1 ;
				if (ProcessInTable.ArrCheck[hor][ver] == 0) {
					Cocaro.mainStage_mc["Checked" + hor.toString() + ver.toString() + "_mc"].visible = true ;
					Cocaro.mainStage_mc["Checked" + hor.toString() + ver.toString() + "_mc"].over_mc.visible = false ;
				} else {
					while (ver > 0) {
						ver = ver - 1 ;
						if (ProcessInTable.ArrCheck[hor][ver] != 0) {
							if (ProcessInTable.ArrCheck[hor][ver] != obj.Color)
								Search_Chess(hor, ver) ;
							break ;
						}
					}
					break ;
				}
			}
			//}
		}
	}
}